package com.xidian.zhiancloud.controller.equipFacility.eqFacMng;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xidian.zhiancloud.constant.EquipStatus;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dto.equipFacility.EqFacMngDTO;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.DeviceType;
import com.xidian.zhiancloud.model.EqFacMng;
import com.xidian.zhiancloud.model.EqFacMngRecord;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.qo.equipFacility.DefendPlanMakeQO;
import com.xidian.zhiancloud.qo.equipFacility.EqFacMngQO;
import com.xidian.zhiancloud.service.equipFacility.DeviceTypeService;
import com.xidian.zhiancloud.service.equipFacility.EqFacMngRecordService;
import com.xidian.zhiancloud.service.equipFacility.EqFacMngService;
import com.xidian.zhiancloud.service.systemSetting.SysPositionService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeVO;
import com.xidian.zhiancloud.vo.equipFacility.EqFacMngDTOVO;
import com.xidian.zhiancloud.vo.equipFacility.EqFacMngVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 设备设施 设备设施管理接口
 *
 * @Author: 任启亮
 * @Date: 2020/11/11
 */
@RestController
@RequestMapping("/EqFacMng")
public class EqFacMngController extends BaseController {
    @Autowired
    private EqFacMngService eqFacMngService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    @Autowired
    private EqFacMngRecordService eqFacMngRecordService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private SysPositionService sysPositionService;
    /**
     * 新增设备设施管理表
     *
     * @param eqFacMngDTO 设备设施管理对象
     * @return 返回是否添加成功
     */
    @PostMapping("/add")
    public ResultDTO<Boolean> add(EqFacMngDTO eqFacMngDTO) {
        eqFacMngDTO.getEqFacMng().setCompanyId(getSessionUserCompanyId());
        eqFacMngDTO.getEqFacMng().setCreator(getSessionUserid());
        Boolean result = eqFacMngService.add(eqFacMngDTO);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id获得设备设施管理单条记录
     *
     * @param id 设备设施管理表的主键
     * @return 设备设施管理根据主键查询的单条数据
     */
    @GetMapping("/getById")
    public ResultDTO<EqFacMngDTOVO> getById(@RequestParam("id") Long id) {
        return ResultDTO.successOf(eqFacMngService.getDTOVOById(id), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id删除设备设施管理表数据
     *
     * @param ids 对应主键值
     * @return 返回删除标志
     */
    @PostMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        Boolean result = eqFacMngService.deleteByIds(ids);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改设备设施管理数据
     *
     * @param eqFacMngDTO 设备设施管理对象
     * @return 返回修改状态
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(EqFacMngDTO eqFacMngDTO) {
        eqFacMngDTO.getEqFacMng().setCompanyId(null);
        eqFacMngDTO.getEqFacMng().setCreator(null);
        EqFacMng eqFacMng = eqFacMngDTO.getEqFacMng();
        EqFacMng old = eqFacMngService.getById(eqFacMng.getId());
        if(old == null){
            return ResultDTO.successOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        List<EqFacMngRecord> records = new LinkedList<>();
        String username = getSessionUser().getName();
        if (!eqFacMng.getName().equals(old.getName())) {
            EqFacMngRecord record = new EqFacMngRecord();
            record.setEqFacMngId(eqFacMng.getId());
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            record.setRecord(username + "更改设备名称为" + eqFacMng.getName());
            records.add(record);
        }
        if (!eqFacMng.getDeviceTypeId().equals(old.getDeviceTypeId())) {
            EqFacMngRecord record = new EqFacMngRecord();
            record.setEqFacMngId(eqFacMng.getId());
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            DeviceType deviceType = deviceTypeService.getById(eqFacMng.getDeviceTypeId());
            if (deviceType == null) {
                return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
            }
            record.setRecord(username + "更改设备类型为" + deviceType.getName());
            records.add(record);
        }
        if (!eqFacMng.getMngCtrDeptId().equals(old.getMngCtrDeptId())) {
            EqFacMngRecord record = new EqFacMngRecord();
            record.setEqFacMngId(eqFacMng.getId());
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            Organization organization = organizationService.getById(eqFacMng.getMngCtrDeptId());
            if(organization == null){
                return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
            }
            record.setRecord(username + "更改管辖部门为" +organization.getName());
            records.add(record);
        }
        if(!eqFacMng.getPosition().equals(old.getPosition())){
            EqFacMngRecord record = new EqFacMngRecord();
            record.setEqFacMngId(eqFacMng.getId());
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            SysPosition sysPosition = sysPositionService.getById(eqFacMng.getPosition());
            if(sysPosition == null){
                return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
            }
            record.setRecord(username + "更改区域位置为" +sysPosition.getName());
            records.add(record);
        }
        if(!eqFacMng.getEquipStatus().equals(old.getEquipStatus())){
            EqFacMngRecord record = new EqFacMngRecord();
            record.setEqFacMngId(eqFacMng.getId());
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            record.setRecord(username + "更改设备状态为" + EquipStatus.statusType.get(eqFacMng.getEquipStatus()));
            records.add(record);
        }
        eqFacMngRecordService.saveBatch(records);
        Boolean result = eqFacMngService.patchById(eqFacMngDTO);
        return ResultDTO.successOf(result, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列表查询
     *
     * @param eqFacMngQO 传递参数
     * @return 返回分页列表
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<EqFacMngVO>> getListByQO(EqFacMngQO eqFacMngQO) {
        if (eqFacMngQO.getCurrent() == null || eqFacMngQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        eqFacMngQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(eqFacMngService.getListByQO(eqFacMngQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 变更记录
     *
     * @param id
     * @return
     */
    @GetMapping("/getRecords")
    public ResultDTO<List<EqFacMngRecord>> getRecords(@RequestParam("id") Long id) {
        LambdaQueryWrapper<EqFacMngRecord> query = Wrappers.<EqFacMngRecord>lambdaQuery();
        query.eq(EqFacMngRecord::getEqFacMngId, id).orderByDesc(true,EqFacMngRecord::getUpdateTime);
        List<EqFacMngRecord> list = eqFacMngRecordService.list(query);
        return ResultDTO.successOf(list, GeneralResponseEnums.SUCCESS);
    }
}
